package cn.cjj.gulimall.ware.mq;

import cn.cjj.common.to.OrderEntityTo;
import cn.cjj.common.to.mq.StockLockTo;
import cn.cjj.gulimall.ware.entity.WareOrderTaskDetailEntity;
import cn.cjj.gulimall.ware.entity.WareOrderTaskEntity;
import cn.cjj.gulimall.ware.feign.OrderFeignService;
import cn.cjj.gulimall.ware.service.WareOrderTaskDetailService;
import cn.cjj.gulimall.ware.service.WareOrderTaskService;
import cn.cjj.gulimall.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author CuiJunJie
 * @create 2021/7/13-14:39
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockDelayQueue {

    @Autowired
    WareOrderTaskService wareOrderTaskService;

    @Autowired
    WareOrderTaskDetailService wareOrderTaskDetailService;

    @Autowired
    OrderFeignService orderFeignService;

    @Autowired
    WareSkuService wareSkuService;

    @RabbitHandler
    public void stockLockRelease(StockLockTo stockLockTo, Message message, Channel channel) throws IOException {
        System.out.println("库存解锁");
        try {
            wareSkuService.unLock(stockLockTo);
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

    @RabbitHandler
    public void orderCloseRelease(OrderEntityTo orderEntityTo, Message message, Channel channel) throws IOException {
        System.out.println("订单解锁");
        try {
            wareSkuService.unLockStock(orderEntityTo);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
